﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Box2D.XNA;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

///<Summary>
///A simple wrapper over Box2D's world class to keep this 
///module seperate from our actual TheWorld Class
///this will be a subset of TheWorld
///</Summary>


namespace Imagine
{
    
    public class PhysicsWorld : DefaultContactListener
    {
        #region Private_Parts
        bool            m_drawDebugData;
        float           m_fixedTimeStep;
        float           m_step;
        DebugDraw       m_debugDraw;
        World           m_physicalWorld;
        #endregion

        public PhysicsWorld( Vector2 gravity, bool sleep)
        {
            m_physicalWorld = new World(gravity, sleep/*Makes inactive objects sleep*/);
            m_physicalWorld.ContactListener = this;
            m_fixedTimeStep = 1.0f/60.0f;        //So that we dont have to do floating division again and again
            m_step = 0.0f;
            m_debugDraw = new DebugDraw();
            m_physicalWorld.DebugDraw = m_debugDraw;    //Assign the debugDraw but need not render it
            
        }

        public void Step(GameTime time)
        {
             m_physicalWorld.Step(m_fixedTimeStep, /*Velocity Iterations*/8,/*Position Iterations*/3 );
            //TODO : to be done later after i introduce effects
            if(m_drawDebugData)
               m_physicalWorld.DrawDebugData();
        }

        public void Draw(GameTime time)
        {
            m_debugDraw.FinishDrawShapes();
            m_debugDraw.FinishDrawString();
        }

        #region Properties
        public bool DrawDebugData
        {
            get { return m_drawDebugData;    }
            set { m_drawDebugData = value;   }
        }
        public World Box2DWorld
        {
            get {   return m_physicalWorld;    }
        }
        #endregion
    }
}
